#include <iostream>
#include <unistd.h>
using namespace std;

int g_val = 100;

void Test1(){
  pid_t id = fork();
  if(id == 0)
  {
    //child:
    int cnt = 0;
    while(1)
    {
      cout << "I'm child process "<< "pid:" << getpid() << " ppid:" << getppid();
      cout << "g_val:" << g_val << " &g_val:" << &g_val << endl;
      sleep(1);
      ++cnt;
      if(cnt == 3)
      {
        g_val = 200;
        cout << "child change g_val 100->200 success!" << endl;
      }
    }
  }
  else{
    //father:
    while(1)
    {
      cout << "I'm father process "<< "pid:" << getpid() << " ppid:" << getppid();
      cout << "g_val:" << g_val << " &g_val:" << &g_val << endl;
      sleep(1);
    }
  }
}

int g_init_val = 10;
int g_uninit_val;

int main(int argc, char** argv, char** env){
  //代码区
  cout << "code addr:" << (void*)main << endl;
  cout << "const string addr:" << (void*)"hello" << endl;
  cout << endl;
  //初始化数据区
  cout << "init global addr:" << &g_init_val << endl;
  static int s_init_val = 20;
  cout << "init static addr:" << &s_init_val << endl; 
  //未初始化数据区
  static int s_uninit_val;
  cout << "uninit global addr:" << &g_uninit_val << endl;
  cout << "uninit static addr:" << &s_uninit_val << endl;
  cout << endl;
  //堆区和栈区
  int* pheap1 = new int;
  int* pheap2 = new int;
  int* pheap3 = new int;
  cout << "heap addr:" << pheap1 << endl;
  cout << "heap addr:" << pheap2 << endl;
  cout << "heap addr:" << pheap3 << endl;
  cout << "stack addr:" << &pheap1 << endl;
  cout << "stack addr:" << &pheap2 << endl;
  cout << "stack addr:" << &pheap3 << endl;
  cout << endl;
  //命令行参数和环境变量
  cout << "argv addr:" << (void*)argv[0] << endl;
  cout << "env addr:" << (void*)env[0] << endl;
  return 0;
}
